#import "ALElementColumnEntry.h"
#import "ALElement.h"
#import "NSObject+BlockObservation.h"


#pragma mark Constants
NSString* const ALElementTypeDidChangeNotification = @"ALElementTypeDidChangeNotification";


#pragma mark -
@interface ALElementColumnEntry (ALElementColumnEntryPrivateMethods)
-(void)updateLabelColor_;
@end


#pragma mark -
@implementation ALElementColumnEntry

@synthesize valueView;
@synthesize labelView;
@synthesize textPadding;

@synthesize labelNormalPrimaryColor;
@synthesize labelNormalSecondaryColor;
@synthesize labelHighlightedPrimaryColor;
@synthesize labelHighlightedSecondaryColor;

@synthesize valueNormalPrimaryColor;
@synthesize valueNormalSecondaryColor;
@synthesize valueHighlightedPrimaryColor;
@synthesize valueHighlightedSecondaryColor;


-(id)copyWithZone:(NSZone *)zone {
    ALElementColumnEntry *copy = [super copyWithZone:zone];
    [copy setLabelNormalPrimaryColor:[self labelNormalPrimaryColor]];
    [copy setLabelNormalSecondaryColor:[self labelNormalSecondaryColor]];
    [copy setLabelHighlightedPrimaryColor:[self labelHighlightedPrimaryColor]];
    [copy setLabelHighlightedSecondaryColor:[self labelHighlightedSecondaryColor]];

    [copy setValueNormalPrimaryColor:[self valueNormalPrimaryColor]];
    [copy setValueNormalSecondaryColor:[self valueNormalSecondaryColor]];
    [copy setValueHighlightedPrimaryColor:[self valueHighlightedPrimaryColor]];
    [copy setValueHighlightedSecondaryColor:[self valueHighlightedSecondaryColor]];
    
    return copy;
}

-(void)awakeFromNib {
	if (![[self valueView] respondsToSelector:@selector(font)])
		return;
	NSControl *controlView = (NSControl *)[self valueView];
	CGFloat fontSize = [[controlView font] pointSize];
	CGFloat valueViewHeight = [[self valueView] frame].size.height;
	[self setTextPadding:valueViewHeight - fontSize];	
}


#pragma mark -
#pragma mark Column entry overrides

-(void)columnEntryWasAddedToColumnView {
	[super columnEntryWasAddedToColumnView];
	[self updateLabelColor_];
	AMBlockTask labelColorTask = ^(id object, NSDictionary *change) {
		[self updateLabelColor_];
	};
	NSString *lineNumberKey = [NSString stringWithFormat:@"%@.%@", ALRepresentedObjectKey, ALLineNumberKey];
	[self addObserverForKeyPath:lineNumberKey task:labelColorTask];
	[[self parentEntry] addObserverForKeyPath:ALSelectedKey task:labelColorTask];

	AMBlockTask elementTypeTask = ^(id object, NSDictionary *change) {
		NSNumber *newElementType = [[self representedObject] elementType];
		if (!newElementType)
			return;
		NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
		[noteCenter postNotificationName:ALElementTypeDidChangeNotification object:self];
	};
	NSString *elementTypeKey = [NSString stringWithFormat:@"%@.%@", ALRepresentedObjectKey, ALElementTypeKey];
	[self addObserverForKeyPath:elementTypeKey task:elementTypeTask];
	NSString *textWrapsKey = [NSString stringWithFormat:@"%@.%@.%@", ALRepresentedObjectKey, ALTemplateKey, ALTextPropertyWrapsKey];
	[self addObserverForKeyPath:textWrapsKey task:elementTypeTask];
}

-(CGFloat)preferredHeightForWidth:(CGFloat)width {
	BOOL elementIsPrimary = [self elementIsPrimary];
	CGFloat fontSize = elementIsPrimary ? ALElementFontSizePrimary : ALElementFontSizeSecondary;
	CGFloat viewHeight = fontSize + [self textPadding];
	return viewHeight;
}

-(NSView *)willForwardHitTestAtPoint:(NSPoint)aPoint toSubview:(NSView *)subview {
	if (subview == [self valueView] && [[self valueView] respondsToSelector:@selector(isEditable)] && [[[self valueView] valueForKey:@"isEditable"] boolValue]) {
		return subview;
	}
	return [self columnView];
}


#pragma mark -
#pragma mark View attributes

-(void)updateLabelColor_ {
	NSColor *labelNormalColor = [self elementIsPrimary] ? [self labelNormalPrimaryColor] : [self labelNormalSecondaryColor];
	NSColor *labelHighlightedColor = [self elementIsPrimary] ? [self labelHighlightedPrimaryColor] : [self labelHighlightedSecondaryColor];
	NSColor *labelCurrentColor = [self ancestorIsSelected] ? labelHighlightedColor : labelNormalColor;
	[[self labelView] setTextColor:labelCurrentColor];
    
    NSColor *valueNormalColor = [self elementIsPrimary] ? [self valueNormalPrimaryColor] : [self valueNormalSecondaryColor];
    NSColor *valueHighlightedColor = [self elementIsPrimary] ? [self valueHighlightedPrimaryColor] : [self valueHighlightedSecondaryColor];
    NSColor *valueCurrentColor = [self ancestorIsSelected] ? valueHighlightedColor : valueNormalColor;
    if ([[self valueView] respondsToSelector:@selector(setTextColor:)])
        [[self valueView] setValue:valueCurrentColor forKey:@"textColor"];
    else if ([[self valueView] respondsToSelector:@selector(setRatingColor:)])
        [[self valueView] setValue:valueCurrentColor forKey:@"ratingColor"];
}

+(NSSet *)keyPathsForValuesAffectingElementIsPrimary {
	NSString *lineNumberKey = [NSString stringWithFormat:@"%@.%@", ALRepresentedObjectKey, ALLineNumberKey];
	return [NSSet setWithObject:lineNumberKey];
}

-(BOOL)elementIsPrimary {
	if ([[(ALElement *)[self representedObject] lineNumber] integerValue] == 0)
		return YES;
	return NO;
}

+(NSSet *)keyPathsForValuesAffectingElementFontSize {
	NSString *lineNumberKey = [NSString stringWithFormat:@"%@.%@", ALRepresentedObjectKey, ALLineNumberKey];
	return [NSSet setWithObject:lineNumberKey];
}

-(NSInteger)elementFontSize {
	if ([self elementIsPrimary])
		return ALElementFontSizePrimary;
	return ALElementFontSizeSecondary;
}

@end
